package com.google.android.gms.internal;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Parcel;
import android.support.annotation.Nullable;
import android.util.Base64;
import com.google.android.exoplayer2.C;
import com.google.android.gms.ads.internal.zzbt;
import com.google.android.gms.common.internal.Hide;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Hide
@zzabh
public final class zzuo {
    private final Map<zzup, zzuq> zzcdz = new HashMap();
    private final LinkedList<zzup> zzcea = new LinkedList<>();
    @Nullable
    private zztk zzceb;

    private static void zza(String str, zzup zzup) {
        if (zzahw.zzae(2)) {
            zzahw.v(String.format(str, new Object[]{zzup}));
        }
    }

    private static String[] zzax(String str) {
        try {
            String[] split = str.split("\u0000");
            for (int i = 0; i < split.length; i++) {
                split[i] = new String(Base64.decode(split[i], 0), C.UTF8_NAME);
            }
            return split;
        } catch (UnsupportedEncodingException unused) {
            return new String[0];
        }
    }

    private static boolean zzay(String str) {
        try {
            return Pattern.matches((String) zzlc.zzio().zzd(zzoi.zzbpy), str);
        } catch (RuntimeException e) {
            zzbt.zzep().zza(e, "InterstitialAdPool.isExcludedAdUnit");
            return false;
        }
    }

    private static String zzaz(String str) {
        try {
            Matcher matcher = Pattern.compile("([^/]+/[0-9]+).*").matcher(str);
            if (matcher.matches()) {
                return matcher.group(1);
            }
        } catch (RuntimeException unused) {
        }
        return str;
    }

    private static void zzb(Bundle bundle, String str) {
        while (true) {
            String[] split = str.split("/", 2);
            if (split.length != 0) {
                String str2 = split[0];
                if (split.length == 1) {
                    bundle.remove(str2);
                    return;
                }
                bundle = bundle.getBundle(str2);
                if (bundle != null) {
                    str = split[1];
                } else {
                    return;
                }
            } else {
                return;
            }
        }
    }

    static Set<String> zzh(zzkk zzkk) {
        HashSet hashSet = new HashSet();
        hashSet.addAll(zzkk.extras.keySet());
        Bundle bundle = zzkk.zzbhf.getBundle("com.google.ads.mediation.admob.AdMobAdapter");
        if (bundle != null) {
            hashSet.addAll(bundle.keySet());
        }
        return hashSet;
    }

    static zzkk zzi(zzkk zzkk) {
        zzkk zzk = zzk(zzkk);
        Bundle bundle = zzk.zzbhf.getBundle("com.google.ads.mediation.admob.AdMobAdapter");
        if (bundle != null) {
            bundle.putBoolean("_skipMediation", true);
        }
        zzk.extras.putBoolean("_skipMediation", true);
        return zzk;
    }

    private static zzkk zzj(zzkk zzkk) {
        zzkk zzk = zzk(zzkk);
        for (String str : ((String) zzlc.zzio().zzd(zzoi.zzbpu)).split(",")) {
            zzb(zzk.zzbhf, str);
            if (str.startsWith("com.google.ads.mediation.admob.AdMobAdapter/")) {
                zzb(zzk.extras, str.replaceFirst("com.google.ads.mediation.admob.AdMobAdapter/", ""));
            }
        }
        return zzk;
    }

    private static zzkk zzk(zzkk zzkk) {
        Parcel obtain = Parcel.obtain();
        zzkk.writeToParcel(obtain, 0);
        obtain.setDataPosition(0);
        zzkk createFromParcel = zzkk.CREATOR.createFromParcel(obtain);
        obtain.recycle();
        return ((Boolean) zzlc.zzio().zzd(zzoi.zzbpi)).booleanValue() ? createFromParcel.zzhz() : createFromParcel;
    }

    private final String zzlh() {
        try {
            StringBuilder sb = new StringBuilder();
            Iterator it = this.zzcea.iterator();
            while (it.hasNext()) {
                sb.append(Base64.encodeToString(((zzup) it.next()).toString().getBytes(C.UTF8_NAME), 0));
                if (it.hasNext()) {
                    sb.append("\u0000");
                }
            }
            return sb.toString();
        } catch (UnsupportedEncodingException unused) {
            return "";
        }
    }

    /* access modifiers changed from: package-private */
    @Nullable
    public final zzur zza(zzkk zzkk, String str) {
        if (zzay(str)) {
            return null;
        }
        int i = new zzaeb(this.zzceb.getApplicationContext()).zzom().zzcyo;
        zzkk zzj = zzj(zzkk);
        String zzaz = zzaz(str);
        zzup zzup = new zzup(zzj, zzaz, i);
        zzuq zzuq = this.zzcdz.get(zzup);
        if (zzuq == null) {
            zza("Interstitial pool created at %s.", zzup);
            zzuq = new zzuq(zzj, zzaz, i);
            this.zzcdz.put(zzup, zzuq);
        }
        this.zzcea.remove(zzup);
        this.zzcea.add(zzup);
        zzuq.zzll();
        while (true) {
            if (this.zzcea.size() <= ((Integer) zzlc.zzio().zzd(zzoi.zzbpv)).intValue()) {
                break;
            }
            zzup remove = this.zzcea.remove();
            zzuq zzuq2 = this.zzcdz.get(remove);
            zza("Evicting interstitial queue for %s.", remove);
            while (zzuq2.size() > 0) {
                zzur zzl = zzuq2.zzl((zzkk) null);
                if (zzl.zzcek) {
                    zzus.zzln().zzlp();
                }
                zzl.zzceg.zzdk();
            }
            this.zzcdz.remove(remove);
        }
        while (zzuq.size() > 0) {
            zzur zzl2 = zzuq.zzl(zzj);
            if (zzl2.zzcek) {
                if (zzbt.zzes().currentTimeMillis() - zzl2.zzcej > 1000 * ((long) ((Integer) zzlc.zzio().zzd(zzoi.zzbpx)).intValue())) {
                    zza("Expired interstitial at %s.", zzup);
                    zzus.zzln().zzlo();
                }
            }
            String str2 = zzl2.zzceh != null ? " (inline) " : " ";
            StringBuilder sb = new StringBuilder(34 + String.valueOf(str2).length());
            sb.append("Pooled interstitial");
            sb.append(str2);
            sb.append("returned at %s.");
            zza(sb.toString(), zzup);
            return zzl2;
        }
        return null;
    }

    /* access modifiers changed from: package-private */
    public final void zza(zztk zztk) {
        if (this.zzceb == null) {
            this.zzceb = zztk.zzlf();
            if (this.zzceb != null) {
                SharedPreferences sharedPreferences = this.zzceb.getApplicationContext().getSharedPreferences("com.google.android.gms.ads.internal.interstitial.InterstitialAdPool", 0);
                while (this.zzcea.size() > 0) {
                    zzup remove = this.zzcea.remove();
                    zzuq zzuq = this.zzcdz.get(remove);
                    zza("Flushing interstitial queue for %s.", remove);
                    while (zzuq.size() > 0) {
                        zzuq.zzl((zzkk) null).zzceg.zzdk();
                    }
                    this.zzcdz.remove(remove);
                }
                try {
                    HashMap hashMap = new HashMap();
                    for (Map.Entry next : sharedPreferences.getAll().entrySet()) {
                        if (!((String) next.getKey()).equals("PoolKeys")) {
                            zzuu zzba = zzuu.zzba((String) next.getValue());
                            zzup zzup = new zzup(zzba.zzarx, zzba.zzapp, zzba.zzcee);
                            if (!this.zzcdz.containsKey(zzup)) {
                                this.zzcdz.put(zzup, new zzuq(zzba.zzarx, zzba.zzapp, zzba.zzcee));
                                hashMap.put(zzup.toString(), zzup);
                                zza("Restored interstitial queue for %s.", zzup);
                            }
                        }
                    }
                    for (String str : zzax(sharedPreferences.getString("PoolKeys", ""))) {
                        zzup zzup2 = (zzup) hashMap.get(str);
                        if (this.zzcdz.containsKey(zzup2)) {
                            this.zzcea.add(zzup2);
                        }
                    }
                } catch (IOException | RuntimeException e) {
                    zzbt.zzep().zza(e, "InterstitialAdPool.restore");
                    zzahw.zzc("Malformed preferences value for InterstitialAdPool.", e);
                    this.zzcdz.clear();
                    this.zzcea.clear();
                }
            }
        }
    }

    /* access modifiers changed from: package-private */
    public final void zzb(zzkk zzkk, String str) {
        if (this.zzceb != null) {
            int i = new zzaeb(this.zzceb.getApplicationContext()).zzom().zzcyo;
            zzkk zzj = zzj(zzkk);
            String zzaz = zzaz(str);
            zzup zzup = new zzup(zzj, zzaz, i);
            zzuq zzuq = this.zzcdz.get(zzup);
            if (zzuq == null) {
                zza("Interstitial pool created at %s.", zzup);
                zzuq = new zzuq(zzj, zzaz, i);
                this.zzcdz.put(zzup, zzuq);
            }
            zzuq.zza(this.zzceb, zzkk);
            zzuq.zzll();
            zza("Inline entry added to the queue at %s.", zzup);
        }
    }

    /* access modifiers changed from: package-private */
    /* JADX WARNING: Code restructure failed: missing block: B:8:0x002f, code lost:
        r5 = r1.size();
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final void zzlg() {
        /*
            r9 = this;
            com.google.android.gms.internal.zztk r0 = r9.zzceb
            if (r0 != 0) goto L_0x0005
            return
        L_0x0005:
            java.util.Map<com.google.android.gms.internal.zzup, com.google.android.gms.internal.zzuq> r0 = r9.zzcdz
            java.util.Set r0 = r0.entrySet()
            java.util.Iterator r0 = r0.iterator()
        L_0x000f:
            boolean r1 = r0.hasNext()
            r2 = 0
            if (r1 == 0) goto L_0x0089
            java.lang.Object r1 = r0.next()
            java.util.Map$Entry r1 = (java.util.Map.Entry) r1
            java.lang.Object r3 = r1.getKey()
            com.google.android.gms.internal.zzup r3 = (com.google.android.gms.internal.zzup) r3
            java.lang.Object r1 = r1.getValue()
            com.google.android.gms.internal.zzuq r1 = (com.google.android.gms.internal.zzuq) r1
            r4 = 2
            boolean r5 = com.google.android.gms.internal.zzahw.zzae(r4)
            if (r5 == 0) goto L_0x0056
            int r5 = r1.size()
            int r6 = r1.zzlj()
            if (r6 >= r5) goto L_0x0056
            java.lang.String r7 = "Loading %s/%s pooled interstitials for %s."
            r8 = 3
            java.lang.Object[] r8 = new java.lang.Object[r8]
            int r6 = r5 - r6
            java.lang.Integer r6 = java.lang.Integer.valueOf(r6)
            r8[r2] = r6
            java.lang.Integer r5 = java.lang.Integer.valueOf(r5)
            r6 = 1
            r8[r6] = r5
            r8[r4] = r3
            java.lang.String r4 = java.lang.String.format(r7, r8)
            com.google.android.gms.internal.zzahw.v(r4)
        L_0x0056:
            int r4 = r1.zzlk()
            int r2 = r2 + r4
        L_0x005b:
            int r4 = r1.size()
            com.google.android.gms.internal.zzny<java.lang.Integer> r5 = com.google.android.gms.internal.zzoi.zzbpw
            com.google.android.gms.internal.zzog r6 = com.google.android.gms.internal.zzlc.zzio()
            java.lang.Object r5 = r6.zzd(r5)
            java.lang.Integer r5 = (java.lang.Integer) r5
            int r5 = r5.intValue()
            if (r4 >= r5) goto L_0x0081
            java.lang.String r4 = "Pooling and loading one new interstitial for %s."
            zza((java.lang.String) r4, (com.google.android.gms.internal.zzup) r3)
            com.google.android.gms.internal.zztk r4 = r9.zzceb
            boolean r4 = r1.zzb((com.google.android.gms.internal.zztk) r4)
            if (r4 == 0) goto L_0x005b
            int r2 = r2 + 1
            goto L_0x005b
        L_0x0081:
            com.google.android.gms.internal.zzus r1 = com.google.android.gms.internal.zzus.zzln()
            r1.zzu(r2)
            goto L_0x000f
        L_0x0089:
            com.google.android.gms.internal.zztk r0 = r9.zzceb
            if (r0 == 0) goto L_0x00ea
            com.google.android.gms.internal.zztk r0 = r9.zzceb
            android.content.Context r0 = r0.getApplicationContext()
            java.lang.String r1 = "com.google.android.gms.ads.internal.interstitial.InterstitialAdPool"
            android.content.SharedPreferences r0 = r0.getSharedPreferences(r1, r2)
            android.content.SharedPreferences$Editor r0 = r0.edit()
            r0.clear()
            java.util.Map<com.google.android.gms.internal.zzup, com.google.android.gms.internal.zzuq> r1 = r9.zzcdz
            java.util.Set r1 = r1.entrySet()
            java.util.Iterator r1 = r1.iterator()
        L_0x00aa:
            boolean r2 = r1.hasNext()
            if (r2 == 0) goto L_0x00de
            java.lang.Object r2 = r1.next()
            java.util.Map$Entry r2 = (java.util.Map.Entry) r2
            java.lang.Object r3 = r2.getKey()
            com.google.android.gms.internal.zzup r3 = (com.google.android.gms.internal.zzup) r3
            java.lang.Object r2 = r2.getValue()
            com.google.android.gms.internal.zzuq r2 = (com.google.android.gms.internal.zzuq) r2
            boolean r4 = r2.zzlm()
            if (r4 == 0) goto L_0x00aa
            com.google.android.gms.internal.zzuu r4 = new com.google.android.gms.internal.zzuu
            r4.<init>(r2)
            java.lang.String r2 = r4.zzlw()
            java.lang.String r4 = r3.toString()
            r0.putString(r4, r2)
            java.lang.String r2 = "Saved interstitial queue for %s."
            zza((java.lang.String) r2, (com.google.android.gms.internal.zzup) r3)
            goto L_0x00aa
        L_0x00de:
            java.lang.String r1 = "PoolKeys"
            java.lang.String r2 = r9.zzlh()
            r0.putString(r1, r2)
            r0.apply()
        L_0x00ea:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.google.android.gms.internal.zzuo.zzlg():void");
    }
}
